﻿using BZhanHengjicvWPF.Common;
using BZhanHengjicvWPF.Common.Models;
using BZhanHengjicvWPF.Extensions;
using BZhanHengjicvWPF.Services;
using BZhanHengjicvWPF.ViewModels;
using BZhanHengjicvWPF.Views;
using BZhanHengjicvWPF.Views.Dialogs;
using DryIoc;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace BZhanHengjicvWPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        /// <summary>
        /// 程序启动时，打开的窗体
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        protected override Window CreateShell()
        {
            return Container.Resolve<MainView>();
        }

        protected override void OnInitialized()
        {
            var diaLog = Container.Resolve<IDialogService>();
            diaLog.ShowDialog("LoginView",
                callback =>
                {
                    if (callback.Result != ButtonResult.OK)
                    {
                        //Application.Current.Shutdown();
                        Environment.Exit(0);
                        return;
                    };
                    var service = App.Current.MainWindow.DataContext as IConfigureService;
                    if (service != null) { service.Configure(); }
                });
            base.OnInitialized();
        }

        public static void LogOut(IContainerProvider containerProvider)
        {
            Current.MainWindow.Hide();
            var dialog = containerProvider.Resolve<IDialogService>();
            dialog.ShowDialog("LoginView", callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }
                Current.MainWindow.Show();
            });
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.GetContainer()
                .Register<HttpRestClient>(made: Parameters.Of.Type<string>(serviceKey: "webUrl"));
            containerRegistry.GetContainer().RegisterInstance(@"http://localhost:36796/", serviceKey: "webUrl");

            containerRegistry.Register<ILoginService,LoginService>();
            containerRegistry.Register<IToDoService,ToDoService>();
            containerRegistry.Register<IMemoService, MemoService>();
            containerRegistry.Register<IDialogHostService,DialogHostService>();

            containerRegistry.RegisterForNavigation<AddToDoView,AddToDoViewModel>();
            containerRegistry.RegisterForNavigation<AddMemoView,AddMemoViewModel>();

            containerRegistry.RegisterForNavigation<AboutView>();
            containerRegistry.RegisterForNavigation<LoginView, LoginViewModel>();
            containerRegistry.RegisterForNavigation<SkinView,SkinViewModel>();
            containerRegistry.RegisterForNavigation<MainView,MainViewModel>();
            containerRegistry.RegisterForNavigation<ToDoView, ToDoViewModel>();
            containerRegistry.RegisterForNavigation<IndexView, IndexViewModel>();
            containerRegistry.RegisterForNavigation<MemoView, MemoViewModel>();
            containerRegistry.RegisterForNavigation<SettingsView, SettingsViewModel>();
            containerRegistry.RegisterForNavigation<MsgView,MsgViewModel>(); 
            containerRegistry.RegisterForNavigation<MsgView,MsgViewModel>();
        }
    }
}
